﻿using TiaoTiaoCode.Common.Enums;
using TiaoTiaoCode.Modbuser.Interfaces;
using TiaoTiaoCode.Modbuser.Modbusers;

namespace TiaoTiaoCode.Modbuser
{
    /// <summary>
    /// Modbuser构建器
    /// </summary>
    public static class ModbuserCreator
    {
        /// <summary>
        /// 创建ModbusTcp
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="dataFormate"></param>
        /// <param name="station"></param>
        /// <param name="addressStartWithZero"></param>
        /// <param name="checkMessageId"></param>
        /// <param name="stringReverse"></param>
        /// <returns></returns>
        public static IModbuser CreateTcp(string ip = "127.0.0.1", ushort port = 502,
            ByteDataFormatType dataFormate = ByteDataFormatType.CDAB, byte station = 1,
            bool addressStartWithZero = true, bool checkMessageId = true, bool stringReverse = false)
        {
            var bus = new TcpModbuser();
            bus.SetConnectInfo(ip, port)
                .SetStation(station)
                .SetByteDataFormat(dataFormate)
                .SetAddressStartWithZero(addressStartWithZero)
                .SetCheckMessageId(checkMessageId)
                .SetIsStringReverse(stringReverse);

            return bus;
        }
    }
}
